package com.daye.homework.servlet;

import com.daye.homework.bean.Student;
import com.daye.homework.factory.AdminServiceFactory;
import com.daye.homework.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "SendUpdateStudentServlet" , urlPatterns = "/sendUpdateStudent")
public class SendUpdateStudentServlet extends HttpServlet {
    private AdminService adminService = AdminServiceFactory.getAdminService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        Integer id = Integer.parseInt(request.getParameter("id"));
        String studentNum = request.getParameter("studentNum");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String desc = request.getParameter("desc");


        Student student = new Student(studentNum , name , sex , birthday , email , desc);
        student.setId(id);

        try {
            adminService.updateStudentById(student);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response.sendRedirect("queryStudent");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
